<?php

namespace app\api\controller\v2;

use app\api\controller\Common;
use app\api\model\ChatSticker;
use think\Db;
use think\Request;

class Sticker extends Common {
    /**
     * @var mixed
     */
    public $params = null;
    /**
     * @param Request $request
     */
    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->params = getRsaPostParamArr2();
    }

    /**
     * @return mixed
     */
    public function all() {
        return $this->pass([]);
        $data = Db::name('chat_sticker')
            ->where(['user_id' => 0, 'is_hidden' => 0])
            ->field('id,name,preview image')
            ->order('sort desc, id asc')
            ->cache('StickerAll',86400)
            ->select();
        return $this->pass($data);
    }

    /**
     * @return mixed
     */
    public function search() {
        $keyword = $this->params['keyword'] ?? '1';
        if (!$keyword) {
            return $this->pass([]);
        }

        if (mb_strlen($keyword) >= 5) {
            return $this->pass([]);
        }

        $data = Db::name('chat_sticker')->where(['name' => $keyword])->field('id,name,preview image')->select();
        return $this->pass($data);
    }

    /**
     * @return mixed
     */
    public function addface() {
        $path = $this->params['path'] ?? '';
        if (!$path) {
            return $this->miss('文件异常');
        }

        $count = Db::name('chat_sticker')->master()->where(['user_id' => $this->member['id']])->count();
        if ($count >= 100) {
            return $this->miss('表情库已满');
        }

        $has = Db::name('chat_sticker')->master()->where(['user_id' => $this->member['id'], 'image' => $path])->find();
        if ($has) {
            return $this->miss('已在表情库');
        }

        $lastsort = Db::name('chat_sticker')->master()->where(['user_id' => $this->member['id']])->order('sort desc, id asc')->value('sort');
        $lastsort = $lastsort ?: 0;

        $res = Db::name('chat_sticker')->insertGetId([
            'user_id' => $this->member['id'],
            'image'   => $path,
            'preview' => $path,
            'sort'    => $lastsort + 1,
        ]);
        cache('userface:' . $this->member['id'], null);

        return $res ? $this->pass(['id'=>$res,'msg'=>'添加成功']) : $this->miss('添加失败');
    }

    /**
     * @return mixed
     */
    public function myface() {
        $cacheKey = 'userface:' . $this->member['id'];
        $data     = cache($cacheKey);
        if (!$data) {
            $data = Db::name('chat_sticker')
                ->where(['cate_id'=>0, 'user_id' => $this->member['id'], 'is_hidden' => 0])
                ->field('id,image')
                ->order('sort desc, id asc')
                ->select();
            cache($cacheKey, $data, 3600);
        }

        return $this->pass($data);
    }

    /**
     * @return mixed
     */
    public function delface() {
        $ids = $this->params['ids'] ?? '';
        $id  = Db::name('chat_sticker')->master()->where(['id' => ['in', $ids], 'user_id' => $this->member['id']])->column('id');
        if (empty($id)) {
            return $this->miss('表情不存在');
        }

        $res = Db::name('chat_sticker')->delete($id);
        cache('userface:' . $this->member['id'], null);
        return $res ? $this->pass('删除成功') : $this->miss('删除失败');
    }

    /**
     * @return mixed
     */
    public function moveface() {
        $ids = $this->params['ids'] ?? '';
        if (!$ids) {
            return $this->miss('参数异常');
        }

        $data = Db::name('chat_sticker')
            ->where(['id' => ['in', $ids], 'user_id' => $this->member['id'], 'is_hidden' => 0])
            ->orderRaw("field(id,$ids)")
            ->column('id');
        if (sizeof(explode(',', $ids)) != sizeof($data)) {
            return $this->miss('数据异常');
        }

        $update = [];
        $sort   = 1;
        for ($i = sizeof($data); $i > 0; $i--) {
            $update[] = [
                'id'   => $data[$i - 1],
                'sort' => $sort,
            ];
            $sort++;
        }
        $res = (new ChatSticker)->saveAll($update);
        if (!$res) {
            return $this->miss('排序失败');
        }

        cache('userface:' . $this->member['id'], null);
        return $this->pass('修改成功');
    }
}